package com.situ.mall.pattern.singleton;

//懒汉式：线程安全问题
public class Singleton2 {
    private static Singleton2 instance;

    private Singleton2() {}

    //懒汉式的双重检查机制
    public static Singleton2 getInstance() {
        if (instance == null) {
            synchronized (Singleton2.class) {
                if (instance == null) {
                    instance = new Singleton2();
                }
            }
        }
        return instance;
    }

    //不管有没有线程安全问题，都加了锁，效率低
    public synchronized static Singleton2 getInstance1() {
        if (instance == null) {
            //用到时候再new
            instance = new Singleton2();
        }
        return instance;
    }
}
